package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.LocalTax;
import com.edward.model.OverseaTax;
import com.edward.model.SelfHarvest;


public class LocalTaxManager extends HibernateDaoSupport {
	
	public void addLocalTax(LocalTax localTax) {
		
		getHibernateTemplate().save(localTax);
	}
	
	@SuppressWarnings("unchecked")
	public void updateLocalTax(LocalTax localTax) {
		
		getHibernateTemplate().update(localTax);
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(localTax.getDate());
		String m = mm.format(localTax.getDate());
		double sum = 0;
		List<OverseaTax> otaxList = getHibernateTemplate().find("from OverseaTax o where year(o.date) = '" + y + "' and month(o.date) = '" + m + "'and o.flag = 't'"  );
		if(otaxList.size()>0){
			sum += otaxList.get(0).getSum();
		}
		List<LocalTax> ltaxList = getHibernateTemplate().find("from LocalTax l where year(l.date) = '" + y + "' and month(l.date) = '" + m + "'and l.flag = 't'"  );
		if(ltaxList.size()>0){
			sum += ltaxList.get(0).getSum();
		}
		addTaxHarvest(sum, localTax.getDate());
	}
	
	@SuppressWarnings("unchecked")
	public void deleteLocalTax(int id){
		LocalTax l = getLocalTax(id);
		Date date = l.getDate();
		getHibernateTemplate().delete(l);
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(date);
		String m = mm.format(date);
		double sum = 0;
		List<OverseaTax> otaxList = getHibernateTemplate().find("from OverseaTax o where year(o.date) = '" + y + "' and month(o.date) = '" + m + "'and o.flag = 't'"  );
		if(otaxList.size()>0){
			sum += otaxList.get(0).getSum();
		}
		List<LocalTax> ltaxList = getHibernateTemplate().find("from LocalTax l where year(l.date) = '" + y + "' and month(l.date) = '" + m + "'and l.flag = 't'"  );
		if(ltaxList.size()>0){
			sum += ltaxList.get(0).getSum();
		}
		addTaxHarvest(sum, date);
	}
	
	public LocalTax getLocalTax(int id){
		LocalTax d =(LocalTax)getHibernateTemplate().get(LocalTax.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<LocalTax> listLocalTax() {
		return getHibernateTemplate().find("from LocalTax");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <LocalTax>list = getHibernateTemplate().find("from LocalTax" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <LocalTax>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query) {
		String y = query.substring(0,4);
		String m = query.substring(5,7);
		String sql = "from LocalTax l where year(l.date) = '" + y + "' and month(l.date) = '" + m + "'and l.flag is null order by l.date" ;
		List<LocalTax> list = getHibernateTemplate().find(sql);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <LocalTax>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
	}
		
	

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerSumList(int start,int limit,String query) {
		if(query.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			query = sdf.format(new Date());
		}
		
		List <LocalTax>list = getHibernateTemplate().find("from LocalTax l where year(l.date) = '" + query + "' and l.flag = 't' order by l.date");
		LocalTax l = new LocalTax(); 
		for (LocalTax lt : list) {
			l.setCct(l.getCct() + lt.getCct());
			l.setPet(l.getPet() + lt.getPet());
			l.setSt(l.getSt() + lt.getSt());
			l.setCet(l.getCet() + lt.getCet());
			l.setVat(l.getVat() + lt.getVat());
			l.setSum(l.getSum()+lt.getSum());
		 }
		l.setDate(null);
		list.add(l);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <LocalTax>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	//判断有没有该月份的合计，如果没有就添加 设type 为T
	public void addcheck (LocalTax lt){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(lt.getDate());
		String m = mm.format(lt.getDate());
		List<OverseaTax> otaxList = getHibernateTemplate().find("from OverseaTax o where year(o.date) = '" + y + "' and month(o.date) = '" + m + "'and o.flag = 't'"  );
		double sum = 0 ;
		if(otaxList.size()>0){
			sum = otaxList.get(0).getSum();
		}
		List<LocalTax> list = getHibernateTemplate().find("from LocalTax l where year(l.date) = '" + y + "' and month(l.date) = '" + m + "'and l.flag = 't'"  );
		if(list.size()==0){
			lt.setFlag("t");
			lt.setId(0);
			addLocalTax(lt);
			sum += lt.getSum();
			addTaxHarvest(sum, lt.getDate());
		}
		else{
			LocalTax l = list.get(0);
			l.setCct(l.getCct() + lt.getCct());
			l.setPet(l.getPet() + lt.getPet());
			l.setSt(l.getSt() + lt.getSt());
			l.setCet(l.getCet() + lt.getCet());
			l.setVat(l.getVat() + lt.getVat());
			l.setSum(l.getSum()+lt.getSum());
			
			sum += l.getSum();
			addTaxHarvest(sum, lt.getDate());
		}
		
		
	}
	
	@SuppressWarnings("unchecked")
	public LocalTax getSum(Date date){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(date);
		String m = mm.format(date);
		List<LocalTax> list = getHibernateTemplate().find("from LocalTax l where year(l.date) = '" + y + "' and month(l.date) = '" + m + "'and l.flag = 't'"  );
		return list.get(0);
	}
	
	
	@SuppressWarnings("unchecked")
	public void addTaxHarvest(double sum,Date date){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(date);
		String m = mm.format(date);
		List <SelfHarvest> list = getHibernateTemplate().find("from SelfHarvest s where year(s.date) = '" + y + "' and month(s.date) = '" + m + "'");
		if(list.size() != 0){
			for(SelfHarvest sh : list){
				if(sh.getType() == null){
					sh.setTax(sum);
					sh.setNetProfit(sh.getIncome() - sh.getCost() - sh.getFee() - sh.getTax() + sh.getBalance() - sh.getOther() - sh.getLoss());
					if(sh.getNetProfit()==0||sh.getIncome()==0){
						sh.setRate(0);
					}else{
						sh.setRate(sh.getNetProfit()/sh.getIncome()*100);
						int i = 0;
						double d ;
						i = (int) (sh.getRate() * 1000);
						d = i;
						sh.setRate(d/1000);
					}
					getHibernateTemplate().save(sh);
				}else{
					sh.setTax(sum/5);
					sh.setNetProfit(sh.getIncome() - sh.getCost() - sh.getFee() - sh.getTax() + sh.getBalance() - sh.getOther() - sh.getLoss());
					if(sh.getNetProfit()==0||sh.getIncome()==0){
						sh.setRate(0);
					}else{
						sh.setRate(sh.getNetProfit()/sh.getIncome()*100);
						int i = 0;
						double d ;
						i = (int) (sh.getRate() * 1000);
						d = i;
						sh.setRate(d/1000);
					}
					getHibernateTemplate().save(sh);
				}
			}
			
		}else{
			SelfHarvest sh = new SelfHarvest();
			List <SelfHarvest> l = new ArrayList<SelfHarvest>();
			sh.setDate(date);
			sh.setTax(sum);
			sh.setSelfs(l);
			getHibernateTemplate().save(sh);
			for(int i=0; i<5;i++){
					SelfHarvest st = new SelfHarvest();
					st.setType(judge(i));
					st.setDate(date);
					st.setTax(sum/5);
					getHibernateTemplate().save(st);
					sh.getSelfs().add(st);
			}
			getHibernateTemplate().save(sh);
		}
		
		
		
	}
	
	public String judge (int i){
		if(i == 0){
			return "汽车";
		}else if (i == 1){
			return "摩托车";
		}else if (i == 2){
			return "汽车配件";
		}else if (i == 3){
			return "摩托车配件";
		}else{
			return "其他";
		}
	}
	


}



